package com.zycfc.zsf.boot.validation.autoconfigure;

import org.springframework.web.servlet.config.annotation.*;
import org.springframework.boot.context.properties.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.validation.beanvalidation.*;
import com.zycfc.zsf.boot.util.text.*;
import org.hibernate.validator.resourceloading.*;
import javax.validation.*;
import org.hibernate.validator.spi.resourceloading.*;
import org.springframework.context.annotation.*;
import org.springframework.validation.*;
import org.slf4j.*;
import org.hibernate.validator.messageinterpolation.*;
import java.util.*;

@Configuration
@EnableConfigurationProperties({ ValidatorProperties.class })
public class ValidatorAutoConfiguration implements WebMvcConfigurer
{
    private static final Logger log;
    @Autowired
    private ValidatorProperties validatorProperties;
    private static final Map<String, String> VALIDATION_ANNATATION_DEFAULT_MESSAGES;
    
    @Bean
    public LocalValidatorFactoryBean zsfValidator() {
        final LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        final List<String> list = new ArrayList<String>();
        list.add("ValidationMessages");
        String extendResourceName = this.validatorProperties.getExtendResourceName();
        if (!StringUtils.isEmpty(extendResourceName)) {
            if (extendResourceName.endsWith("properties")) {
                extendResourceName = extendResourceName.substring(0, extendResourceName.length() - 11);
            }
            ValidatorAutoConfiguration.log.debug("\u52a0\u8f7d\u3010" + extendResourceName + "\u3011\u81ea\u5b9a\u4e49\u9519\u8bef\u4fe1\u606f\u914d\u7f6e\u6587\u4ef6\u6210\u529f");
            list.add(extendResourceName);
        }
        final ResourceBundleLocator locator = (ResourceBundleLocator)new AggregateResourceBundleLocator((List)list);
        factoryBean.setMessageInterpolator((javax.validation.MessageInterpolator)new MessageInterpolator(locator));
        return factoryBean;
    }
    
    public Validator getValidator() {
        final LocalValidatorFactoryBean factoryBean = this.zsfValidator();
        ValidatorAutoConfiguration.log.info("^_^\u52a0\u8f7d\u53c2\u6570\u6548\u9a8c\u7ec4\u4ef6\u6210\u529f^_^");
        return (Validator)factoryBean;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ValidatorAutoConfiguration.class);
        VALIDATION_ANNATATION_DEFAULT_MESSAGES = new HashMap<String, String>(200) {
            {
                this.put("Min", "validation.message.Min.message");
                this.put("NotNull", "javax.validation.constraints.NotNull.message");
                this.put("NotBlank", "javax.validation.constraints.NotBlank.message");
                this.put("AssertFalse", "javax.validation.constraints.AssertFalse.message");
                this.put("AssertTrue", "javax.validation.constraints.AssertTrue.message");
                this.put("DecimalMax", "javax.validation.constraints.DecimalMax.message");
                this.put("DecimalMin", "javax.validation.constraints.DecimalMin.message");
                this.put("Digits", "javax.validation.constraints.Digits.message");
                this.put("Max", "javax.validation.constraints.Max.message");
                this.put("NullL", "javax.validation.constraints.Null.message");
                this.put("Size", "javax.validation.constraints.Size.message");
                this.put("Length", "org.hibernate.validator.constraints.Length.message");
                this.put("Email", "org.hibernate.validator.constraints.Email.message");
                this.put("URL", "org.hibernate.validator.constraints.URL.message");
                this.put("IdNumber", "com.zsf.validator.constraints.IdNumber.message");
                this.put("Phone", "com.zsf.validator.constraints.Phone.message");
            }
        };
    }
    
    private class MessageInterpolator extends ResourceBundleMessageInterpolator
    {
        public MessageInterpolator(final ResourceBundleLocator userResourceBundleLocator) {
            super(userResourceBundleLocator);
        }
        
        public String interpolate(String message, final javax.validation.MessageInterpolator.Context context, final Locale locale) {
            final String annotationTypeName = context.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName();
            final String annotationDefaultMessageCode = ValidatorAutoConfiguration.VALIDATION_ANNATATION_DEFAULT_MESSAGES.get(annotationTypeName);
            ValidatorAutoConfiguration.log.debug("\u6ce8\u89e3\u7c7b\u578b:" + annotationTypeName + ",message=" + message + ",annotationDefaultMessageCode=" + annotationDefaultMessageCode);
            if (null != annotationDefaultMessageCode && !message.startsWith("{javax.validation") && !message.startsWith("{org.hibernate.validator.constraints")) {
                message = message + "{" + annotationDefaultMessageCode + "}";
            }
            return super.interpolate(message, context, locale);
        }
    }
}
